package com.tanjun;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.tanjun.data.entity.BaseEntity;
import com.zaxxer.hikari.HikariDataSource;

import java.io.File;
import java.nio.file.Paths;

public class Main {
    //需要生产的表面
    private static final String[] TABLE_NAME = {"base_admin_menu","base_admin_role_menu"};
    //服务名称
    private static final ServiceEnum serviceEnum = ServiceEnum.USER;

    private static final String MODULE="menu";
    public static void main(String[] args) {
//        String path= Paths.get(System.getProperty("user.dir")).toString();
//        System.out.println(path);
//        if(1==1){
//            return;
//        }

        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://106.54.21.31:3306/mystore?characterEncoding=utf-8");
        dataSource.setUsername("root");
        dataSource.setPassword("1q2w#E$R");

        //创建配置内容，两种风格都可以。
        GlobalConfig globalConfig = createGlobalConfigUseStyle(serviceEnum,MODULE);
        globalConfig.setGenerateTable(TABLE_NAME);

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);

        //生成代码
        generator.generate();
    }


    public static GlobalConfig createGlobalConfigUseStyle(ServiceEnum serviceEnum,String module) {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        String path= Paths.get(System.getProperty("user.dir"))
                + File.separator
                +serviceEnum.getServiceName()
                +File.separator
                +serviceEnum.getServiceName()+"-web"
                +File.separator
                +"src/main/java";

        globalConfig.setSourceDir(path);
        //设置根包 com.tanjun.userservice.web.orm.entity
        globalConfig.setBasePackage(serviceEnum.getBasePackage()+".web.service."+module);

        //设置表前缀和只生成哪些表

        //设置生成 entity 并启用 Lombok
        globalConfig.setEntityGenerateEnable(true);
        globalConfig.setEntityWithLombok(true);
        globalConfig.setEntityLombokAllArgsConstructorEnable(false);
        globalConfig.setEntityLombokNoArgsConstructorEnable(false);
        globalConfig.setEntityBaseOverwriteEnable(true);//存在可以覆盖
        globalConfig.setEntityClassSuffix("Entity");
        globalConfig.setEntitySuperClass(BaseEntity.class);
        globalConfig.setEntityJdkVersion(17);
        //设置mapper
        globalConfig.setMapperGenerateEnable(true);
        globalConfig.setMapperOverwriteEnable(true);
        globalConfig.setMapperAnnotation(true);

        //设置service
        globalConfig.setServiceGenerateEnable(true);
        globalConfig.setServicePackage(globalConfig.getBasePackage());


        //设置serviceImpl
        globalConfig.setServiceImplGenerateEnable(true);
        globalConfig.setServiceImplCacheExample(false);
        globalConfig.setServiceImplPackage(globalConfig.getBasePackage()+".impl");



        //设置项目的JDK版本，项目的JDK为14及以上时建议设置该项，小于14则可以不设置



        //可以单独配置某个列

        //设置生成 mapper
//        globalConfig.setMapperGenerateEnable(true);
//        globalConfig.setMapperPackage("mapper");
//
//        //设置service
//        globalConfig.setServiceGenerateEnable(true);
//        globalConfig.setServiceImplGenerateEnable(true);
        ColumnConfig enableColumn=new ColumnConfig();
        enableColumn.setPropertyType("EnableStatusEnum");
        enableColumn.setColumnName("enabled");
        globalConfig.setColumnConfig(enableColumn);
        return globalConfig;
    }
}